package com.baizhi.cmfzmanager.controller;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.baizhi.cmfzmanager.Annotation.LogAnnotation;
import com.baizhi.cmfzmanager.entity.Guru;
import com.baizhi.cmfzmanager.service.GuruService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class GuruController {
    @Autowired
    private GuruService guruService;

    @LogAnnotation("分页查询大师")
    @RequestMapping("/getGurus")
    public Map getGurus(int page,int rows){
        return guruService.getGurus(page,rows);
    }
    @LogAnnotation("查询所有大师")
    @RequestMapping("/getAll")
    public List<Guru> getAll(){
        return guruService.getAll();
    }
    @LogAnnotation("修改大师信息")
    @RequestMapping("/updateGuru")
    public Map updateGuru(Guru guru, MultipartFile imagefile, HttpServletRequest request) throws IOException {
        Map map = new HashMap();
        try {
            if(!imagefile.isEmpty()) {
                String imagefileName = imagefile.getName();
                ServletContext servletContext = request.getServletContext();
                String realPath = servletContext.getRealPath("/img");

                imagefile.transferTo(new File(realPath+"/"+imagefileName));
                guru.setGuruImage("/img/"+imagefileName);
            }
            guruService.update(guru);
            map.put("message","修改成功！！！");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("message","修改失败！！！");
        }
        return map;
    }

    @LogAnnotation("添加大师方法")
    @RequestMapping("/addGuru")
    public Map addGuru(Guru guru,MultipartFile imagefile,HttpServletRequest request) throws IOException {
        Map map = new HashMap();
        try {
            String imagefileName = imagefile.getOriginalFilename();
            ServletContext servletContext = request.getServletContext();
            String realPath = servletContext.getRealPath("/img");

            imagefile.transferTo(new File(realPath+"/"+imagefileName));
            guru.setGuruImage("/img/"+imagefileName);
            guruService.add(guru);
            map.put("message","添加成功！！！");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("message","添加失败！！！");

        }

        return map;
    }

    @LogAnnotation("删除大师方法")
    @RequestMapping("/deleteGuru")
    public Map deleteGuru(int[] ids){
        Map map = new HashMap();

        try {
            guruService.delete(ids);
            map.put("message","删除成功！！！");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("message","删除失败！！！");
        }

        return map;
    }

    @LogAnnotation("导入大师表格方法")
    @RequestMapping("/addTable")
    public Map addTable(MultipartFile gurutable) {
        System.out.println(gurutable.isEmpty());
        Map map = new HashMap();
        try {
            ImportParams importParams = new ImportParams();
            // 设置标题占行数
            importParams.setTitleRows(1);
            // 设置表头占的行数
            importParams.setHeadRows(1);
            //参数 1.读取文件 2.对应的实体类 3.导入参数对象
            // pojo 普通Java对象 vo view Object dto 中间对象 do
            List<Guru> list = ExcelImportUtil.importExcel(gurutable.getInputStream(),
                    Guru.class, importParams);
            guruService.addlist(list);
            map.put("message","添加成功！！！");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("message","添加失败！！！");
        }

        return map;
    }

}
